package pacoteDao;

import java.sql.*;
import java.util.*;
import projetofinal.ConnectionFactory;
import projetofinal.Disciplina;

public class DisciplinaDAO {

    private ConnectionFactory conexao = new ConnectionFactory();
    private Connection connection;
    String query;

    public DisciplinaDAO() throws SQLException {
        if (connection == null || connection.isClosed()) {
            this.connection = conexao.getConnection();
        }
    }

    public boolean insert(Disciplina disciplina) throws SQLException {

        query = "insert into Disciplina(id,nome,idprofessor) values (" + disciplina.getId() + ",'" + disciplina.getNome() + "'," + disciplina.getIdprofessor() + ")";
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean update(Disciplina disciplina) throws SQLException {

        query = "update Disciplina set nome = '" + disciplina.getNome() + "', idprofessor = " + disciplina.getIdprofessor() + " where id = " + disciplina.getId();
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean delete(Disciplina disciplina) throws SQLException {

        query = "delete from Disciplina where id = " + disciplina.getId();
        int linhas = conexao.executarNoQuery(query);

        conexao.close();
        return linhas > 0;

    }

    public List<Disciplina> consultar() throws SQLException {

        query = "select * from Disciplina ";
        ResultSet rset = conexao.executar(query);

        List<Disciplina> disciplinas = null;
        while (rset.next()) {
            disciplinas = new ArrayList<Disciplina>();
            Disciplina disciplina = new Disciplina();
            disciplina.setId(rset.getInt(1));
            disciplina.setNome(rset.getString(2));
            disciplina.setIdprofessor(rset.getInt(3));
            disciplinas.add(disciplina);
        }

        conexao.close();
        return disciplinas;

    }

    public Disciplina getById(Integer id) throws SQLException {

        query = "select * from Disciplina where id = " + id;
        ResultSet rs = conexao.executar(query);

        Disciplina disciplina = null;
        if (rs.next()) {
            disciplina = new Disciplina();
            disciplina.setId(rs.getInt(1));
            disciplina.setNome(rs.getString(2));
            disciplina.setIdprofessor(rs.getInt(3));
        }

        conexao.close();
        return disciplina;

    }

    public Disciplina consultaNome(String nome) throws SQLException {

        query = "select * from Disciplina where nome = '" + nome + "'";
        ResultSet rs = conexao.executar(query);

        Disciplina disciplina = null;
        if (rs.next()) {
            disciplina = new Disciplina();
            disciplina.setId(rs.getInt(1));
            disciplina.setNome(rs.getString(2));
            disciplina.setIdprofessor(rs.getInt(3));
        }

        conexao.close();
        return disciplina;

    }

    public List<Disciplina> getByIdAluno(Integer aluno) throws SQLException {

        query = "select * from Disciplina where idaluno = " + aluno;
        ResultSet rset = conexao.executar(query);

        List<Disciplina> disciplinas = null;
        while (rset.next()) {
            disciplinas = new ArrayList<Disciplina>();
            Disciplina disciplina = new Disciplina();
            disciplina.setId(rset.getInt(1));
            disciplina.setNome(rset.getString(2));
            disciplina.setIdprofessor(rset.getInt(3));
            disciplinas.add(disciplina);
        }

        conexao.close();
        return disciplinas;

    }
}
